#!/usr/bin/python3
import shutil
import subprocess
import hashlib
import sys
from tempfile import mkdtemp


# import pyperclip


def md5(fname):
    """
    Получает md5-сумму файла
    :param fname: имя файла
    :return: строка с md5-суммой
    """
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()


def run_test(cmd, md5sum):
    """
    Запускает тест, который меняет файл конфига и проверяет, совпадает ли полученная md5-сумма с ожидаемой.
    В зависимости от этого меняются переменные-счётчики прошедших и неудачных тестов
    :param cmd: команда для проверки
    :param md5sum: ожидаемая md5-сумма
    """
    argv = cmd.split()
    for param in argv:
        if '.ini' in param:
            filename = param
    if filename == 'temp.ini':
        subprocess.run('rm -rf temp.ini', shell=True)
    global fail_cnt
    global testnum
    global failed
    ret = subprocess.run(cmd, shell=True).returncode
    fail = False
    if md5(filename) == md5sum:
        print(f'Test {testnum} ok')
    else:
        print(f"Test {testnum} failed")
        fail = True
        failed.append(testnum)
        # print(filename, md5(filename), md5sum)
    if ret or fail:
        fail_cnt += 1
    testnum += 1


def get_test(cmd, ans):
    """
    Запускает тест, который получает результат из файла конфига и проверяет, совпадает ли он с ожидаемым.
    В зависимости от этого меняются переменные-счётчики прошедших и неудачных тестов
    :param cmd: команда для проверки
    :param ans: ожидаемый результат
    """
    global fail_cnt
    global testnum
    global failed
    ret = subprocess.run(cmd, shell=True, capture_output=True).stdout.decode()
    if ret.strip() != ans.strip():
        fail_cnt += 1
        print(f"Test {testnum} failed")
        failed.append(testnum)
    else:
        print(f'Test {testnum} ok')
    testnum += 1


def retcode_test(cmd, code):
    """
    Запускает тест, который получает код возврата при обработке файла конфига и проверяет, совпадает ли он с ожидаемым.
    В зависимости от этого меняются переменные-счётчики прошедших и неудачных тестов
    :param cmd: команда для проверки
    :param code: ожидаемый код возврата
    """
    global fail_cnt
    global testnum
    global failed
    ret = subprocess.run(cmd, shell=True, capture_output=True).returncode
    if ret != code:
        fail_cnt += 1
        print(f"Test {testnum} failed")
        failed.append(testnum)
    else:
        print(f'Test {testnum} ok')
    testnum += 1


tempdir = mkdtemp()
shutil.copyfile('section_source.ini', f'{tempdir}/section.ini')
shutil.copyfile('nosection_source.ini', f'{tempdir}/nosection.ini')
shutil.copyfile('indent_source.ini', f'{tempdir}/indent.ini')

subprocess.run(['mkdir', '-p', f'{tempdir}/section.ini.d'])
subprocess.run(['mkdir', '-p', f'{tempdir}/nosection.ini.d'])
subprocess.run(['mkdir', '-p', f'{tempdir}/indent.ini.d'])
print('a=4', file=open(f'{tempdir}/nosection.ini.d/t.ini', 'w'))
print('a=5', file=open(f'{tempdir}/nosection.ini.d/w.ini', 'w'))
print('a=6', file=open(f'{tempdir}/indent.ini.d/q.ini', 'w'))
print('[t]\nmos=1147', file=open(f'{tempdir}/section.ini.d/file.conf', 'w'))
shutil.copyfile(f'{tempdir}/section.ini', f'{tempdir}/section.ini.d/r.ini')
shutil.copyfile(f'{tempdir}/section.ini', f'{tempdir}/nosection.ini.d/b.ini')

fail_cnt = 0
testnum = 1
failed = []
# pyperclip.copy(tempdir)

# general
run_test(f'../py-ini-config.py set {tempdir}/section.ini t s 12', '1876c77f977616cc693708233de67199')  # Test 1
run_test(f'../py-ini-config.py set {tempdir}/section.ini g w 14 -s "="', '9948d2b5c70f256d047d7da29fbcf06f')  # Test 2
run_test(f'../py-ini-config.py set {tempdir}/section.ini a b c -s "="', '2796bfc3eb01fe33d85acfd9a4764b24')  # Test 3
run_test(f'../py-ini-config.py del {tempdir}/section.ini g w', 'ddd14fe8c63d765abc4ccd5c73ca1d1d')  # Test 4
run_test(f'../py-ini-config.py del {tempdir}/section.ini a b --flush', '1876c77f977616cc693708233de67199')  # Test 5
run_test(f'../py-ini-config.py del_section {tempdir}/section.ini g', 'bf0075cbf61fb801342b595ae54c19b6')  # Test 6
run_test(f'../py-ini-config.py del_section {tempdir}/section.ini ew', 'aaa33e99009c875066b298c759b8d615')  # Test 7
run_test(f'../py-ini-config.py comment {tempdir}/section.ini t s', 'e59bb48f1dca5226cbf0d999c71f105f')  # Test 8
run_test(f'../py-ini-config.py uncomment {tempdir}/section.ini t s', 'aaa33e99009c875066b298c759b8d615')  # Test 9
run_test(f'../py-ini-config.py comment {tempdir}/section.ini t', '307632035979fbba84a7572650bf64de')  # Test 10
run_test(f'../py-ini-config.py uncomment {tempdir}/section.ini t fg', '013977e480f4a69c9cf921290a8933b7')  # Test 11
run_test(f'../py-ini-config.py set {tempdir}/section.ini d t 16', 'a214f347a2509b6fea5104fdf186ed47')  # Test 12
run_test(f'../py-ini-config.py set {tempdir}/section.ini d t 84 --view-comments',
         '0a0fedaf9d22d79e78e217dc63d16025')  # Test 13
run_test(f'../py-ini-config.py set {tempdir}/section.ini d t 9 --view-comments',
         '97e9362f74d6cc7e52163ef180e2cd6a')  # Test 14
run_test(f'../py-ini-config.py set {tempdir}/section.ini t s 10 --view-comments',
         '0ad345212b5bfe50c25a507924f117bb')  # Test 15
run_test(f'../py-ini-config.py set {tempdir}/section.ini t s 12 --view-comments',
         '9aa4cfb98ae201b2ae09428015e281fb')  # Test 16
get_test(f'../py-ini-config.py get {tempdir}/section.ini d t', '9')  # Test 17
get_test(f'../py-ini-config.py get {tempdir}/section.ini t fg', 'fg')  # Test 18
get_test(f'../py-ini-config.py get_commented {tempdir}/section.ini t s', '10')  # Test 19
get_test(f'../py-ini-config.py get_commented {tempdir}/section.ini d t', '16\n84')  # Test 20
run_test(f'../py-ini-config.py set {tempdir}/section.ini a b c -s "="', '6801a10179ccc4b167c72a3f40ec3be9')  # Test 21
run_test(f'../py-ini-config.py del {tempdir}/section.ini a b', 'cc1ae1c2918ca89119722a8b56aaaff0')  # Test 22
retcode_test(f'../py-ini-config.py is_empty {tempdir}/section.ini a', 0)  # Test 23
retcode_test(f'../py-ini-config.py is_empty {tempdir}/section.ini d', 1)  # Test 24
retcode_test(f'../py-ini-config.py is_empty {tempdir}/section.ini t', 1)  # Test 25
run_test(f'../py-ini-config.py comment {tempdir}/section.ini d t', '3b70130cd029607a7082a97e732933a5')  # Test 26
retcode_test(f'../py-ini-config.py is_empty {tempdir}/section.ini d', 1)  # Test 27
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini d t', 1)  # Test 28
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t fg', 1)  # not set - no value # Test 29
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s', 0)  # Test 30
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini a r', 1)  # Test 31
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini not_a_section none', 1)  # Test 32
# create
run_test(f'../py-ini-config.py set -c {tempdir}/temp.ini section var value -s " : "',
         '84bd3323fb8976d675869018e1c6d3b4')  # Test 33
run_test(f'../py-ini-config.py set -c {tempdir}/temp2.ini section var value',
         'a0fd466bfac749f2d715dc7d0ff55a52')  # Test 34
# indents, semicolon-comment
get_test(f'../py-ini-config.py get {tempdir}/indent.ini a b -s ":"', 'c')  # Test 35
get_test(f'../py-ini-config.py get {tempdir}/indent.ini b b -s ":"', 'c')  # Test 36
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a b z -s ":"', 'ee29baac5e0212fde037acd6d6c1ff78')  # Test 37
get_test(f'../py-ini-config.py get {tempdir}/indent.ini a b -s ":"', 'z')  # Test 38
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a e y -s " : "', 'ea46c5673fe5e7ac848db0ade35d242e')  # Test 39
get_test(f'../py-ini-config.py get {tempdir}/indent.ini a e -s ":"', 'y')  # Test 40
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a g x -s " : " --no-indent',
         'f2bc077eb43fa95c763af8a892d7144d')  # Test 41
get_test(f'../py-ini-config.py get {tempdir}/indent.ini a g -s ":"', 'x')  # Test 42
run_test(f'../py-ini-config.py comment {tempdir}/indent.ini b c -s ":" --semicolon-comment',
         '3ba683e3e86990759cf918c6a6678e7d')  # Test 43
get_test(f'../py-ini-config.py get_commented {tempdir}/indent.ini b c -s ":" --semicolon-comment', 'd')  # Test 44
run_test(f'../py-ini-config.py uncomment {tempdir}/indent.ini b c -s ":" --semicolon-comment',
         'f2bc077eb43fa95c763af8a892d7144d')  # Test 45
run_test(f'../py-ini-config.py comment {tempdir}/indent.ini a -s ":" --semicolon-comment',
         '92b225cbdc9ea9e41bd2eb111faaf66a')  # Test 46
get_test(f'../py-ini-config.py get_commented {tempdir}/indent.ini a d -s ":" --semicolon-comment', 'e')  # Test 47
run_test(f'../py-ini-config.py uncomment {tempdir}/indent.ini a d -s ":" --semicolon-comment',
         'a4e11eb9b8022b508040661959c7dcb3')  # Test 48
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a e w -s ":"', '277e629faa938077ab8933be529fe2db')  # Test 49
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a c v -s ":" --view-comments --semicolon-comment',  # Test 50
         'cab2b940c1938c5c9bfd4cee5e16306a')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a c d -s ":" --view-comments --semicolon-comment',  # Test 51
         '97ae29fadb317dcb3551a077cd2fc655')
run_test(
    f'../py-ini-config.py set {tempdir}/indent.ini a e y -s "  :  " --view-comments --semicolon-comment --no-indent',
    # Test 52
    '2d011426e64ffda1a8adaead72d0510d')
run_test(f'../py-ini-config.py comment {tempdir}/indent.ini b -s ":" --semicolon-comment',
         '53b70550e3a3452021e894a60d205664')  # Test 53
run_test(f'../py-ini-config.py set {tempdir}/indent.ini a b z -s ":" --view-comments --semicolon-comment --no-indent',
         # Test 54
         'c281c40e02c78f57fd32de50ac471e29')
retcode_test(f'../py-ini-config.py is_set {tempdir}/indent.ini -s ":" a c', 0)  # Test 55
retcode_test(f'../py-ini-config.py is_set {tempdir}/indent.ini -s ":" a f', 1)  # Test 56
run_test(
    f'../py-ini-config.py set {tempdir}/indent.ini b c z -s "  :  " --view-comments --semicolon-comment --no-indent',
    # Test 57
    'bb3411d4635906a35aad2bab4b752886')
run_test(f'../py-ini-config.py set {tempdir}/indent.ini b c d -s "  :  " --view-comments --semicolon-comment',
         # Test 58
         '0e50f69d85e24a4ce8b210943b2cb119')
# nosection
get_test(f'../py-ini-config.py get {tempdir}/nosection.ini q --no-section', '1')  # Test 59
get_test(f'../py-ini-config.py get {tempdir}/nosection.ini g --no-section', '14')  # Test 60
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini q 15 --no-section',
         '1fbdf2c2dafbda2483bc715d477c7590')  # Test 61
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini w 16 --no-section',
         '7fa6c288148f9b0db82d5e8e580c21c5')  # Test 62
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini t 17 --no-section --no-indent',
         '4a97e22aafbee934ff5c9fbccd81be90')  # Test 63
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini p 18 --no-section',
         'b5a776c3d9888b6a05ed8fbfa14845c9')  # Test 64
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini i 19 --view-comments --no-section -s " = "',  # Test 65
         '24cc445ff7e34de7134dcdecdcd787fa')
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini i 8 --view-comments --no-section',
         '877fd863da1e631f703f8566f1b8c58d')  # Test 66
get_test(f'../py-ini-config.py get_commented {tempdir}/nosection.ini o --no-section', '9')  # Test 67
get_test(f'../py-ini-config.py get_commented {tempdir}/nosection.ini i --no-section', '19')  # Test 68
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini d --no-section',
         '0356818cd5cfad1978fed96c6628b6fc')  # Test 69
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini --no-section d', 1)  # Test 70
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini --no-section y', 0)  # Test 71
run_test(f'../py-ini-config.py uncomment {tempdir}/nosection.ini d --no-section',
         '877fd863da1e631f703f8566f1b8c58d')  # Test 72
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini o 20 -s "     =" --no-section',
         'da282bf8f803aeb267de6c2e7d4d0c03')  # Test 73
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini o --no-section',
         '13c542c078ce4653c9cb25b8c08a3f0b')  # Test 74
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini o 21 --view-comments --no-section',
         'a557107d5e50712128adbfdedb87265a')  # Test 75
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini o 9 --view-comments --no-section',
         '1f8bf8f4dc79827f78733bd6c209dd80')  # Test 76

# working with several commented vars with one name
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t', '7c1308af16ba50cda297bd1209be7b83')  # Test 77
run_test(f'../py-ini-config.py uncomment {tempdir}/section.ini d t', '5e5d48c443667433bfc5fefe19b88651')  # Test 78
run_test(f'../py-ini-config.py comment {tempdir}/section.ini d t', '7c1308af16ba50cda297bd1209be7b83')  # Test 79
run_test(f'../py-ini-config.py set {tempdir}/section.ini d t 84 --view-comments',
         '2870194f23cca9f2a4fba78d04b4c5ae')  # Test 80
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t', '4e4763e11adfdd37333be6f75f737e19')  # Test 81

# working with several commented vars with one name with no section
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini o --no-section',
         '0b8456802ddc65af5b39fe6f2aa4142c')  # Test 82
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini o 22 --no-section',
         'debab7b1618f61f7e9fc0bc66576e824')  # Test 83
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini o --no-section',
         '4ecb066024b89615ab5a7701c301dc2f')  # Test 84
run_test(f'../py-ini-config.py del {tempdir}/nosection.ini o --no-section',
         '0b8456802ddc65af5b39fe6f2aa4142c')  # Test 85
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini p --no-section',
         '61ffe8ec55a41f122a36d9d920a51336')  # Test 86
run_test(f'../py-ini-config.py uncomment {tempdir}/nosection.ini p --no-section',
         '0b8456802ddc65af5b39fe6f2aa4142c')  # Test 87
run_test(f'../py-ini-config.py comment {tempdir}/nosection.ini p --no-section',
         '61ffe8ec55a41f122a36d9d920a51336')  # Test 88
run_test(f'../py-ini-config.py del {tempdir}/nosection.ini p --no-section',
         'd14285539152ef51144713bd52ba167b')  # Test 89

# deleting empty and not empty sections
run_test(f'../py-ini-config.py del {tempdir}/section.ini a t --flush', '2ea86432eef4f9b8e52a58467a7c6f79')  # Test 90
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t --flush', '2a49205632e29b7bb331a6e42b62abdf')  # Test 91
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t', 'dda8c3c6823cbcd033db1b8f00be6e5f')  # Test 92
run_test(f'../py-ini-config.py del {tempdir}/section.ini d t --flush', 'bdbf48f842f14a62e97aed1afa1e1161')  # Test 93

# for macs: = in param value
run_test(f'../py-ini-config.py set {tempdir}/section.ini t ab cd=ef', '8b89bdcbfa87227cf734391cf3ce60f1')  # Test 94
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t ab', 0)  # Test 95
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t ab=cd', 1)  # Test 96
get_test(f'../py-ini-config.py get {tempdir}/section.ini t ab', 'cd=ef')  # Test 97

# add, is_set for values
run_test(f'../py-ini-config.py add {tempdir}/section.ini t fg 4', 'abc78ec416feb23204b598976a74a73e')  # Test 98
run_test(f'../py-ini-config.py add {tempdir}/section.ini t fg hello', 'fca08c9e2c001ef65def37123adf93c4') # Test 99
run_test(f'../py-ini-config.py add {tempdir}/section.ini t s 14', '2e030b506897b7f9870bdde5ef6ff6b3')  # Test 100
run_test(f'../py-ini-config.py add {tempdir}/section.ini a b c', '7f8a6187904b41656cf39e0eb78b527b')  # Test 101
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini a b c', 0)  # Test 102
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini a b d', 1)  # Test 103
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini a b', 0)  # Test 104
run_test(f'../py-ini-config.py del {tempdir}/section.ini t s 12', 'd691c2b046b12973b898072b5f8b974c')  # Test 105
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s 12', 1)  # Test 106
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s 14', 0)  # Test 107
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s', 0)  # Test 108
run_test(f'../py-ini-config.py del {tempdir}/section.ini t s 14', '319cc141b32267ed996748cd0e6c3382')  # Test 109
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s 14', 1)  # Test 110
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s', 0)  # Test 111
run_test(f'../py-ini-config.py del {tempdir}/section.ini t s', 'f221a25fce13975d2ad2c312542d7d1e')  # Test 112
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s 14', 1)  # Test 113
retcode_test(f'../py-ini-config.py is_set {tempdir}/section.ini t s', 1)  # Test 114

# add, is_set for values without sections
run_test(f'../py-ini-config.py add -n {tempdir}/nosection.ini w 17 -S :',
         'cbe002fe951d69ef0b79188de63f6f1f')  # Test 115
run_test(f'../py-ini-config.py add -n {tempdir}/nosection.ini w 184op -S :',
         '71ead1c575057473bb39722d84d2d68e')  # Test 116
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini -n w -S :', 0)  # Test 117
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini -n e -S :', 0)  # Test 118
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini -n e 3 -S :', 0)  # Test 119
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini -n w 17 -S :', 0)  # Test 120
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini -n w 18 -S :', 1)  # Test 121
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini -n -S : w 184op', 0)  # Test 122
run_test(f'../py-ini-config.py del -n {tempdir}/nosection.ini w 17 -S :',
         'd65bb038cb53300d72fd4234c152c092')  # Test 123
run_test(f'../py-ini-config.py del -n {tempdir}/nosection.ini w 16 -S :',
         '5c62adee2941102cce399e0828e6feae')  # Test 124
run_test(f'../py-ini-config.py add -n {tempdir}/nosection.ini w 17', 'd6217fcc6b9886db34ef511f8e480431')  # Test 125
run_test(f'../py-ini-config.py add -n {tempdir}/nosection.ini w 17', 'd6217fcc6b9886db34ef511f8e480431')  # Test 126
run_test(f'../py-ini-config.py add -n {tempdir}/nosection.ini w 184op', 'd6217fcc6b9886db34ef511f8e480431')  # Test 127
run_test(f'../py-ini-config.py del -n {tempdir}/nosection.ini w 17', '5c62adee2941102cce399e0828e6feae')  # Test 128
run_test(f'../py-ini-config.py del -n {tempdir}/nosection.ini w 184op', '8ec0fff940825118f69e4964c1e23e05')  # Test 129
retcode_test(f'../py-ini-config.py is_set -n {tempdir}/nosection.ini -S : w', 0)  # Test 130
retcode_test(f'../py-ini-config.py is_set -n {tempdir}/nosection.ini -S : w 17', 1)  # Test 131
retcode_test(f'../py-ini-config.py is_set -n {tempdir}/nosection.ini -S : w 184op', 1)  # Test 132

# get with no var
get_test(f'../py-ini-config.py get {tempdir}/section.ini t', 'ab=cd=ef\n#s=10\nfg=4,hello\n#bg=4') # Test 133
get_test(f'../py-ini-config.py get {tempdir}/section.ini a', 'b=c') # Test 134

# rename
run_test(f'../py-ini-config.py rename {tempdir}/section.ini t fg newname23', 'f453d2012622b9dc41fa63359f96bec4')  # Test 135
run_test(f'../py-ini-config.py rename {tempdir}/section.ini a b c', 'fe483568c5ef979d3028bacec389adfd')  # Test 136
run_test(f'../py-ini-config.py rename {tempdir}/temp.ini section -s ":" var kk', '7a080b8928a04748aa3646d9d33154bc')  # Test 137
run_test(f'../py-ini-config.py rename {tempdir}/indent.ini b c variable -s ":"', 'd9069dbf0a0f295327e2b5bbed94fc45')  # Test 138
run_test(f'../py-ini-config.py rename {tempdir}/temp.ini section -s ":" kk kk', '7a080b8928a04748aa3646d9d33154bc')  # Test 139
run_test(f'../py-ini-config.py rename {tempdir}/nosection.ini -n g g[\$i]', '781eab49fdae7e9020217a33cfb169eb')  # Test 140
run_test(f'../py-ini-config.py rename {tempdir}/nosection.ini -n g[\$i] g', '7dd8fe02d0571c3a8b15034be09a24f3')  # Test 141
run_test(f'../py-ini-config.py rename {tempdir}/nosection.ini -n f f', '7dd8fe02d0571c3a8b15034be09a24f3')  # Test 142
run_test(f'../py-ini-config.py rename {tempdir}/nosection.ini -n f my', '986ddbebb0fbc89fa5b42c71d877c58e')  # Test 143
run_test(f'../py-ini-config.py rename {tempdir}/nosection.ini -n w m', 'd72556fbb4e1de2ea092a49fdccc03e8')  # Test 144

# recursive scan
run_test(f'../py-ini-config.py set {tempdir}/section.ini.d/r.ini t s 54', 'f1b636c894c531bb9ca382f5ddbe553c')  # Test 145
get_test(f'../py-ini-config.py get --all {tempdir}/section.ini t s ', '54') # Test 146
run_test(f'../py-ini-config.py set {tempdir}/section.ini t s 80', 'bf7f831be90d7a56e8d6ecbb5dfd303e')  # Test 147
get_test(f'../py-ini-config.py get --all {tempdir}/section.ini t s ', '54') # Test 148
get_test(f'../py-ini-config.py get --all {tempdir}/section.ini t s -i', '80') # Test 149
retcode_test(f'../py-ini-config.py is_set -a {tempdir}/section.ini t mos', 0) # Test 150
retcode_test(f'../py-ini-config.py is_set -a {tempdir}/section.ini t mos -e ini', 1) # Test 151
retcode_test(f'../py-ini-config.py is_set -a {tempdir}/section.ini t mos -e conf', 0) # Test 152
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini a 11 --no-section', '791c39dd8dd2df5be05d20408d25bdf0') # Test 153
get_test(f'../py-ini-config.py get --all {tempdir}/nosection.ini a -i --no-section', '11') # Test 154
get_test(f'../py-ini-config.py get --all {tempdir}/nosection.ini a --no-section', '5') # Test 155
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini.d/t.ini b -n 34', '3149bcaef9441166cd860e850d8cf72e')  # Test 156
retcode_test(f'../py-ini-config.py is_set {tempdir}/nosection.ini b --no-section', 1)  # Test 157
retcode_test(f'../py-ini-config.py is_set -a {tempdir}/nosection.ini b --no-section', 0)  # Test 158
get_test(f'../py-ini-config.py get --all {tempdir}/nosection.ini -a {tempdir}/indent.ini a --no-section', '6') # Test 159
get_test(f'../py-ini-config.py get --all {tempdir}/nosection.ini -a {tempdir}/indent.ini a --no-section -i', '6') # Test 160
get_test(f'../py-ini-config.py get --all {tempdir}/indent.ini -a {tempdir}/nosection.ini a --no-section', '5') # Test 161
get_test(f'../py-ini-config.py get --all {tempdir}/indent.ini -a {tempdir}/nosection.ini a --no-section -i', '11') # Test 162
run_test(f'../py-ini-config.py set {tempdir}/section.ini.d/r.ini t mos 2024', 'b7cab5ae4e7a4ac15de55d90faaa7436')  # Test 163
get_test(f'../py-ini-config.py get --all {tempdir}/section.ini t mos -e conf', '1147') # Test 164
get_test(f'../py-ini-config.py get --all {tempdir}/section.ini t mos -e ini', '2024') # Test 165

#shadowing and sort-collected
run_test(f'../py-ini-config.py set -c {tempdir}/section.ini.d/t.ini -n op 34', 'c9fdecdffb05f395194c5be3c9e29760')  # Test 166
retcode_test(f'../py-ini-config.py is_set -a {tempdir}/nosection.ini --all {tempdir}/section.ini b --no-section --shadowing', 1)  # Test 167
retcode_test(f'../py-ini-config.py is_set -a {tempdir}/section.ini --all {tempdir}/nosection.ini b --no-section --shadowing', 0)  # Test 168
retcode_test(f'../py-ini-config.py is_set -a {tempdir}/nosection.ini --all {tempdir}/section.ini op --no-section --shadowing', 0)  # Test 169
retcode_test(f'../py-ini-config.py is_set -a {tempdir}/section.ini --all {tempdir}/nosection.ini op --no-section --shadowing', 1)  # Test 170
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini.d/t.ini -n op 35', 'dc4cf51b2531c4159a1094129a7f9aa2')  # Test 171
get_test(f'../py-ini-config.py get --all {tempdir}/section.ini -a {tempdir}/nosection.ini op --no-section --shadowing', '35') # Test 172
get_test(f'../py-ini-config.py get --all {tempdir}/nosection.ini -a {tempdir}/section.ini op --no-section --shadowing', '34') # Test 173
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini.d/w.ini -n op 40', 'ba74f89090bb0da84633f8650d96cb2f')  # Test 174
get_test(f'../py-ini-config.py get --all {tempdir}/nosection.ini -a {tempdir}/section.ini op --no-section --sort-collected', '40') # Test 175
run_test(f'../py-ini-config.py set {tempdir}/nosection.ini.d/t.ini -n colshad 46', 'a7ca5bc12887bf405567ab9e3c3253da')  # Test 176
run_test(f'../py-ini-config.py set {tempdir}/section.ini.d/r.ini -n colshad 50', '81e56c96cdc87824e921115fe1ff4e37')  # Test 177
get_test(f'../py-ini-config.py get --all {tempdir}/nosection.ini -a {tempdir}/section.ini colshad --no-section --sort-collected', '46') # Test 178
get_test(f'../py-ini-config.py get --all {tempdir}/nosection.ini -a {tempdir}/section.ini colshad --no-section --sort-collected --shadowing', '50') # Test 179

print(tempdir)
print(f'Failed {fail_cnt} tests')
if fail_cnt > 0:
    print(f'Fails on {failed}')
sys.exit(fail_cnt)
